﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KMouse
{
    class Cmdlist
    {
        public struct CMD
        {
            public const string Test = "Test";

            //操作继电器的
            public const string PC_Boot = "PC_Boot";
            public const string PC_Shutdown = "PC_Shutdown";
            public const string PC_AutoPower = "PC_AutoPower";
            public const string IO_High = "IO_High";
            public const string IO_Low = "IO_Low";

            //延时，BAT,Keyword等功能            
            public const string Delay = "Delay";
            public const string XDelay = "XDelay";
            public const string Bat = "Bat";
            public const string WinReboot = "WinReboot";
            public const string KeywordGet = "KeywordGet";
            public const string KeywordSet = "KeywordSet";
            public const string RunIni = "RunIni";

            //KMouse自身控制的功能
            public const string Identify = "Identify";
            public const string Again = "Again";
            public const string Close = "Close";
            public const string Lock = "Lock";
            public const string Unlock = "Unlock";
            public const string WinSet = "WinSet";

            //鼠标键盘操作
            public const string MouseSet = "MouseSet";
            public const string MouseL = "MouseL";
            public const string MouseDL = "MouseDL";
            public const string MouseR = "MouseR";
            public const string MouseM = "MouseM";
            public const string MouseU = "MouseU";
            public const string MouseD = "MouseD";
            public const string SendKeys = "SendKeys";
        }

        public const string total_command_str = 
         "Test" + "\r\n" +
         "\r\n" +

         "PC_Boot" + "\r\n" +
         "PC_Shutdown" + "\r\n" +
         "PC_AutoPower" + "\r\n" +
         "IO_High" + "\r\n" +
         "IO_Low" + "\r\n" +
         "\r\n" +

         "Delay" + "\r\n" +
         "XDelay" + "\r\n" +
         "Bat" + "\r\n" +
         "WinReboot" + "\r\n" +
         "KeywordGet" + "\r\n" +
         "KeywordSet" + "\r\n" +
         "RunIni" + "\r\n" +
         "\r\n" +

         "Identify" + "\r\n" +
         "Again" + "\r\n" +
         "Close" + "\r\n" +
         "Lock" + "\r\n" +
         "Unlock" + "\r\n" +
         "WinSet" + "\r\n" +
         "\r\n" +

         "MouseSet" + "\r\n" +
         "MouseL" + "\r\n" +
         "MouseDL" + "\r\n" +
         "MouseR" + "\r\n" +
         "MouseM" + "\r\n" +
         "MouseU" + "\r\n" +
         "MouseD" + "\r\n" +
         "SendKeys" + "\r\n" +
         "xxxxxxxxxxx" + "\r\n" +
           "";

        const int MAX_CMD_LIST_LENGTH = 128;
        string[] cmdlist_line;
        string[] cmdlist_command;
        string[] cmdlist_args;
        string[] cmdlist_keyword;
        
        int cmdlist_command_cnt = 0;
        int cmdlist_command_total = 0;
        int command_highlight_offset = 0;

        int cmdlist_keyword_cnt = 0;
        int cmdlist_keyword_total = 0;
        int keyword_highlight_offset = 0;
        
        public int cycle_cnt = 0;

        const int MAX_COMMAND_LENGTH = 1024;    //最大支持多少个命令
        const int MAX_PARAM_PER_COMMAND = 8;    //每个命令最多多少个参数

        MoveCursor mc;

        Form_Main form_main_p;
        FormCmdlist form_cmdlist_p;
        Modbus mdbs_p;

        Queue<string> queue_cmdlist_p;

        System.Timers.Timer timer_execute;
        System.Timers.Timer timer_count_down;

        public void Init(Modbus mdbs_s, Form_Main form_main_s, FormCmdlist form_cmdlist_s, Queue<string> queue_cmdlist_s)
        {
            cmdlist_line = new string[MAX_CMD_LIST_LENGTH];
            cmdlist_keyword = new string[MAX_CMD_LIST_LENGTH];
            cmdlist_command = new string[MAX_CMD_LIST_LENGTH];
            cmdlist_args = new string[MAX_CMD_LIST_LENGTH];            

            timer_execute = new System.Timers.Timer(200);
            timer_execute.Elapsed += new System.Timers.ElapsedEventHandler(timer_execute_Tick);
            timer_execute.AutoReset = true;
            timer_execute.Enabled = false;            

            timer_count_down = new System.Timers.Timer(1000);
            timer_count_down.Elapsed += new System.Timers.ElapsedEventHandler(timer_count_Tick);
            timer_count_down.AutoReset = true;
            timer_count_down.Enabled = false;

            form_main_p = form_main_s;
            form_cmdlist_p = form_cmdlist_s;
            mdbs_p = mdbs_s;
            queue_cmdlist_p = queue_cmdlist_s;

            mc = new MoveCursor();
        }
                
        public void BatCall(Button button_run_s, RichTextBox textBox_Command_s, RichTextBox textBox_Keyword_s,
            TextBox textBox_CmdListShow_s, SerialPort _serialport)
        {
            for(int i = 0; i < Program.parameters.Length; i++)
            {
                Dbg.WriteLine("Raw args[%]:%", i, Program.parameters[i]);
                textBox_CmdListShow_s.Text += i.ToString() + ":" + Program.parameters[i] + "\r\n";

                cmdlist_command_total = 0;
                cmdlist_command_cnt = 0;
                command_highlight_offset = 0;

                cmdlist_line[cmdlist_command_total] = Program.parameters[i];
                cmdlist_command[cmdlist_command_total] = Func_GetCmd(Program.parameters[i]);   //保存命令
                cmdlist_args[cmdlist_command_total] = Func_GetArgs(Program.parameters[i]);     //保存参数

                if((cmdlist_command[cmdlist_command_total] == "") || (cmdlist_args[cmdlist_command_total] == ""))
                {
                    continue;
                }

                if(cmdlist_command[cmdlist_command_total] == CMD.RunIni)
                {
                    Dbg.WriteLine("Runini:%,%!!!", cmdlist_command[cmdlist_command_total], CMD.RunIni);   //直接执行ini文件里的命令

                    Run(button_run_s, textBox_Command_s, textBox_Keyword_s);
                    break;
                }
                cmdlist_command_total++;

                CmdList_Execute(cmdlist_command[cmdlist_command_total],cmdlist_args[cmdlist_command_total]);    //逐条执行命令
            }

            Dbg.WriteLine("Bat run cmd end.");
        }

        private bool Func_CommandIsLegeal(string cmd_string)
        {
            bool is_legeal = false;
            StringReader sr_cmd = new StringReader(total_command_str);
            while(true)
            {
                string str_cmd = sr_cmd.ReadLine();                         //逐行读取
                if(str_cmd == null)
                {
                    break;
                }

                if(str_cmd == cmd_string)
                {
                    is_legeal = true;
                    break;
                }
            }
            sr_cmd.Close();

            return is_legeal;
        }

        private int Func_AnalyzeArgs(string string_value_all, int[] value)
        {
            int value_cnt = 0;
            string string_value_part = "";
            while(true)
            {
                int part_index = string_value_all.IndexOf(",");

                //截取每个","之间的数据作为变量
                if(part_index != -1)
                {
                    string_value_part = string_value_all.Substring(0, part_index);
                    string_value_all = string_value_all.Substring(part_index + 1, string_value_all.Length - part_index - 1);                    

                    //Dbg.WriteLine("@@@At:% PartA:% Left:%", part_index, string_value_part, string_value_all);

                    try
                    {
                        value[value_cnt] = int.Parse(string_value_part);
                        value_cnt++;
                    }
                    catch
                    {
                        return 0;
                    }
                }
                else
                {
                    //Dbg.WriteLine("@@@PartB:%", string_value_all);

                    try
                    {
                        value[value_cnt] = int.Parse(string_value_all);
                        value_cnt++;
                    }
                    catch
                    {
                        return 0;
                    }

                    break;
                }
            }

             return value_cnt;
        }

        private string Func_GetArgs(string str_cmd)
        {
            string string_value_all = "";
            int start_index;
            int end_index;

            start_index = str_cmd.IndexOf("(") + 1;
            end_index = str_cmd.IndexOf(")");

            if((start_index == 0) || (end_index == -1) || (start_index == -1))
            {
                Dbg.WriteLine("args null:%! s:% e:%", str_cmd, start_index, end_index);
                return null;
            }

            if(end_index == start_index)
            {
                return "";
            }

            string_value_all = str_cmd.Substring(start_index, end_index - start_index); //截取括号里的数据
            if(string_value_all == null)
            {
                string_value_all = "";
            }
            return string_value_all;
        }

        private string Func_GetCmd(string str_cmd)
        {
            int end_index;

            end_index = str_cmd.IndexOf("(");
            if(end_index == -1)
            {
                return "";
            }
            return str_cmd.Substring(0, end_index);
        }

        static public string path_lock = ".\\lock";
        private void CmdList_Execute(string cmd, string args)
        {
            Dbg.WriteLine("[%]ExeCMD. Cnt:%|% Cmd:% Args:%", 
                DateTime.Now.ToString("yy/MM/dd HH:mm:ss"), cmdlist_command_cnt, cmdlist_command_total, cmd, args);

            int[] args_value = new int[8];
            int args_num;

            string lock_master = "???";
            if( (File.Exists(path_lock) == true) &&
                ((cmd == CMD.Test) ||
                 (cmd == CMD.Lock) ||
                 (cmd == CMD.Unlock) ||
                 (cmd == CMD.WinSet) || 
                 (cmd == CMD.WinReboot) || 
                 (cmd == CMD.MouseSet) || 
                 (cmd == CMD.MouseL) ||
                 (cmd == CMD.MouseDL) ||
                 (cmd == CMD.MouseR) ||
                 (cmd == CMD.MouseM) ||
                 (cmd == CMD.MouseU) ||
                 (cmd == CMD.MouseD)) )
            {
                StreamReader sr = new StreamReader(path_lock);
                lock_master = sr.ReadLine();
                sr.Close();

                if(lock_master != Param.path_ini_file)                      //不属于自己的锁，则要等待别的kmouse解锁
                {
                    queue_cmdlist_p.Enqueue("Wait [" + lock_master + "] unlock...");

                    int wait_cnt = 0;
                    timer_execute.Stop();
                    while(true)
                    {
                        if(button_run_p.Text == "Run")
                        {
                            break;
                        }

                        if(File.Exists(path_lock) == true)
                        {
                            Dbg.WriteLine("wait lock:% done...", wait_cnt);
                            wait_cnt++;
                            Thread.Sleep(1000);
                        }
                        else
                        {
                            queue_cmdlist_p.Enqueue("Lock [" + lock_master + "] is free at " + wait_cnt.ToString());
                            break;
                        }
                    }
                    timer_execute.Start();
                }
            }

            if(cmd == CMD.Test)
            {
                args_num = Func_AnalyzeArgs(args, args_value);

                if(lock_master != "???")
                {
                    queue_cmdlist_p.Enqueue("Get lock: " + lock_master);
                }

                Dbg.WriteLine("I am CMD Test");

                for(int i = 0; i < args_num; i++)
                {
                    Dbg.WriteLine("\t Args:%", args_value[i]);
                }
            }
            else if(cmd == CMD.PC_Boot)
            {
                //MessageBox.Show("Run Boot!");
                mdbs_p.Send_03(Modbus.REG.MOVEMENT, 1, keyQ.MOUSE.MOVEMENT.BOOT);
            }
            else if(cmd == CMD.PC_Shutdown)
            {
                //MessageBox.Show("Run Shutdown!");
                mdbs_p.Send_03(Modbus.REG.MOVEMENT, 1, keyQ.MOUSE.MOVEMENT.SHUTDOWN);
            }
            else if(cmd == CMD.PC_AutoPower)
            {
                //MessageBox.Show("Run AuotPower!");
                mdbs_p.Send_03(Modbus.REG.MOVEMENT, 1, keyQ.MOUSE.MOVEMENT.AUTOPOWER);
            }
            else if(cmd == CMD.IO_High)
            {
                //MessageBox.Show("Run IO_HIGH!");
                mdbs_p.Send_03(Modbus.REG.MOVEMENT, 1, keyQ.MOUSE.MOVEMENT.IO_High);
            }
            else if(cmd == CMD.IO_Low)
            {
                //MessageBox.Show("Run IO_LOW!");
                mdbs_p.Send_03(Modbus.REG.MOVEMENT, 1, keyQ.MOUSE.MOVEMENT.IO_Low);
            }
            else if(cmd == CMD.Identify)
            {
                mdbs_p.Send_03(Modbus.REG.IDENTIFY, 1, 0);
            }
            else if(cmd == CMD.Delay)
            {
                Func_AnalyzeArgs(args, args_value);

                int XR = args_value[0];

                if(XR > 0)
                {
                    timer_execute.Stop();
                    int delay_cnt = 0;
                    while(true)
                    {
                        if(button_run_p.Text == "Run")
                        {
                            break;
                        }

                        delay_cnt++;
                        queue_cmdlist_p.Enqueue("Delay: " + delay_cnt.ToString() + "/" + XR.ToString());
                        Thread.Sleep(1000);
                        if(delay_cnt == XR)
                        {
                            break;
                        }
                    }
                    queue_cmdlist_p.Enqueue("Delay done");
                    timer_execute.Start();
                }
            }
            else if(cmd == CMD.Bat)
            {
                System.Diagnostics.ProcessStartInfo pinfo = new System.Diagnostics.ProcessStartInfo();
                pinfo.UseShellExecute = true;
                pinfo.FileName = Param.dict["BAT_PATH"].ToString();

                //启动进程
                System.Diagnostics.Process p = System.Diagnostics.Process.Start(pinfo);
            }
            else if(cmd == CMD.KeywordGet)
            {
                form_cmdlist_p.Invoke((EventHandler)(delegate
                {
                    Dbg.WriteLine("Set Clipboard:%", cmdlist_keyword[cmdlist_keyword_cnt]);

                    //Clipboard.SetDataObject(cmd_list_keyboard[keyword_count]);
                    Clipboard.SetText(cmdlist_keyword[cmdlist_keyword_cnt]);

                    //复原上次的颜色与字体
                    RichTextBox_Keyword_p.SelectionColor = Color.Gray; //Color.DarkGray, Color.Silver;
                    RichTextBox_Keyword_p.SelectionFont = new Font(RichTextBox_Keyword_p.Font, FontStyle.Regular);
                    RichTextBox_Keyword_p.Select(keyword_highlight_offset, cmdlist_keyword[cmdlist_keyword_cnt].Length);

                    //更新本次的的颜色与字体
                    RichTextBox_Keyword_p.SelectionColor = Color.Blue;
                    RichTextBox_Keyword_p.SelectionFont = new Font(RichTextBox_Keyword_p.Font, FontStyle.Bold);
                    keyword_highlight_offset += cmdlist_keyword[cmdlist_keyword_cnt].Length + 1;//补上/r和/n

                    //RichTextBox_Keyword_p.Copy();

                    cmdlist_keyword_cnt++;
                    if(cmdlist_keyword_cnt == cmdlist_keyword_total)
                    {
                        cmdlist_keyword_cnt = 0;
                        keyword_highlight_offset = 0;
                    }
                }));
            }
            else if(cmd == CMD.KeywordSet)
            {
                //必须用多线程访问框住，否则get不到信息
                form_cmdlist_p.Invoke((EventHandler)(delegate
                {
                    string iData = Clipboard.GetText();
                    if(iData == null)
                    {
                        Dbg.WriteLine("iData null?");
                    }
                    else
                    {
                        Dbg.WriteLine("Get Clipboard:%", iData);
                    }

                    SendKeys.SendWait(iData);
                }));
            }
            else if(cmd == CMD.SendKeys)
            {
                SendKeys.SendWait(args);
            } 
            else if(cmd == CMD.Lock)
            {
                if(File.Exists(path_lock) == false)
                {
                    StreamWriter sw = File.CreateText(path_lock);
                    sw.WriteLine(Param.path_ini_file);
                    sw.Close();
                }
            }
            else if(cmd == CMD.Unlock)
            {
                if(File.Exists(path_lock) == true)
                {
                    File.Delete(path_lock);
                }
            }
            else if(cmd == CMD.XDelay)
            {
                Func_AnalyzeArgs(args, args_value);

                int XR = args_value[0];
                int YR = args_value[1];

                if((YR > 0) && (YR > XR))
                {
                    Random rd = new Random();
                    int data = rd.Next(XR, YR);

                    Dbg.WriteLine("XDelay from % ~ %, at %", XR, YR, data);

                    timer_execute.Stop();

                    int delay_cnt = 0;
                    while(true)
                    {
                        if(button_run_p.Text == "Run")
                        {
                            break;
                        }

                        delay_cnt++;
                        queue_cmdlist_p.Enqueue("XDelay: " + delay_cnt.ToString() + "/" + data.ToString());
                        Thread.Sleep(1000);
                        if(delay_cnt == data)
                        {
                            break;
                        }
                    }
                    queue_cmdlist_p.Enqueue("XDelay done");

                    timer_execute.Start();
                }
            }
            else if(cmd == CMD.Again)
            {
                if(cycle_cnt + 1 < (int)Param.dict["CMDLIST_CYCLE"])
                {
                    command_highlight_offset = 0;
                    cmdlist_command_cnt = -1;
                    cycle_cnt++;
                }
            }
            else if(cmd == CMD.Close)
            {
                Thread.Sleep(100);
                //form_main_p.Close();//子线程关不掉主线程，得用下面那个
                System.Environment.Exit(0); 
            }
            else if(cmd == CMD.WinReboot)
            {

            }
            else if(cmd == CMD.WinSet)
            {
                Func_AnalyzeArgs(args, args_value);

                int XR = args_value[0];
                int YR = args_value[1];

                Dbg.WriteLine("Windows Set XR:% YR:%", XR, YR);

                form_cmdlist_p.Invoke((EventHandler)(delegate
                {
                    form_cmdlist_p.Location = new System.Drawing.Point(XR, YR);
                }));
            }
            else if(cmd == CMD.MouseSet)
            {
                Func_AnalyzeArgs(args, args_value);

                int XR = args_value[0] * 1000 / Screen.PrimaryScreen.Bounds.Width;
                int YR = args_value[1] * 1000 / Screen.PrimaryScreen.Bounds.Height;

                Dbg.WriteLine("MouseSet XR:% YR:%", XR, YR);

                mc.Mouse_AbsoluteMove(XR, YR);
            }
            else if(cmd == CMD.MouseL)
            {
                mc.Mouse_Single_LeftClick();
            }
            else if(cmd == CMD.MouseDL)
            {
                mc.Mouse_Single_DoubleClick();
            }
            else if(cmd == CMD.MouseR)
            {
                mc.Mouse_RightClick();
            }
            else if(cmd == CMD.MouseM)
            {
                mc.Mouse_MiddleClick();
            }
            else if(cmd == CMD.MouseU)
            {
                mc.Mouse_WheelMove(100);
            }
            else if(cmd == CMD.MouseD)
            {
                mc.Mouse_WheelMove(-100);
            }
            else
            {
                //MessageBox.Show("Unknown CMD!");
                Dbg.WriteLine("##Unknown CMD!");
            }
        }

        const int INIT_COUNT_DOWN_VALUE = 3;
        int count_down_count = INIT_COUNT_DOWN_VALUE;
        Button button_run_p;
        RichTextBox RichTextBox_Command_p;        
        RichTextBox RichTextBox_Keyword_p;
        private void timer_count_Tick(object sender, EventArgs e)
        {
            count_down_count--;
            form_cmdlist_p.Invoke((EventHandler)(delegate
            {
                button_run_p.Text = "Cnt:" + count_down_count.ToString() + "...";
            }));
            if(count_down_count == 0)
            {
                count_down_count = INIT_COUNT_DOWN_VALUE;
                timer_count_down.Enabled = false;

                string command_lines = "";
                form_cmdlist_p.Invoke((EventHandler)(delegate
                {
                    command_lines = RichTextBox_Command_p.Text;
                }));
                
                StringReader sr_cmd = new StringReader(command_lines);
                cmdlist_command_total = 0;
                while(true)
                {
                    string str_cmd = sr_cmd.ReadLine();                     //逐行读取command textbox上的数据
                    if(str_cmd == null)
                    {
                        break;
                    }

                    cmdlist_command[cmdlist_command_total] = str_cmd;              //保存整行
                    cmdlist_line[cmdlist_command_total] = Func_GetCmd(str_cmd);    //保存命令
                    cmdlist_args[cmdlist_command_total] = Func_GetArgs(str_cmd);   //保存参数

                    if( (cmdlist_line[cmdlist_command_total] == "") || 
                        (Func_CommandIsLegeal(cmdlist_line[cmdlist_command_total]) == false) ||
                        (cmdlist_args[cmdlist_command_total] == null) )
                    {
                        MessageBox.Show("Illegal CMD:" + cmdlist_line[cmdlist_command_total] + "!");

                        Reset();
                        sr_cmd.Close();
                        return;
                    }

                    cmdlist_command_total++;
                }
                sr_cmd.Close();

                Dbg.WriteLine("eCMD count:%", cmdlist_command_total);

                if(cmdlist_command_total > 0)
                {
                    Update_Keyword_strline();
                    Execute_Start();        //count down结束后，开启execute定时器
                }
            }
        }

        private void Update_Keyword_strline()
        {
            string keyword_lines = "";
            form_cmdlist_p.Invoke((EventHandler)(delegate
            {
                keyword_lines = RichTextBox_Keyword_p.Text;
            }));

            keyword_highlight_offset = 0;
            StringReader sr_kw = new StringReader(keyword_lines);
            cmdlist_keyword_total = 0;
            cmdlist_keyword_cnt = 0;
            while(true)
            {
                string str_cmd = sr_kw.ReadLine();
                if(str_cmd == null)
                {
                    break;
                }
                cmdlist_keyword[cmdlist_keyword_total] = str_cmd;
                cmdlist_keyword_total++;
            }
            sr_kw.Close();
        }

        private void timer_execute_Tick(object sender, EventArgs e)
        {
            form_cmdlist_p.Invoke((EventHandler)(delegate
            {
                //复原上次的颜色与字体
                RichTextBox_Command_p.SelectionColor = Color.Gray; //Color.DarkGray, Color.Silver;
                RichTextBox_Command_p.SelectionFont = new Font(RichTextBox_Command_p.Font, FontStyle.Regular);
                RichTextBox_Command_p.Select(command_highlight_offset, cmdlist_command[cmdlist_command_cnt].Length);

                //更新本次的的颜色与字体
                RichTextBox_Command_p.SelectionColor = Color.Red;
                RichTextBox_Command_p.SelectionFont = new Font(RichTextBox_Command_p.Font, FontStyle.Bold);
                command_highlight_offset += cmdlist_command[cmdlist_command_cnt].Length + 1;//补上/r和/n
            }));

            CmdList_Execute(cmdlist_line[cmdlist_command_cnt], cmdlist_args[cmdlist_command_cnt]);

            cmdlist_command_cnt++;
            if((cmdlist_command_cnt == cmdlist_command_total) || (cmdlist_command_total == 0))
            {
                Reset();
            }
        }
        
        void Reset()
        {
            Dbg.WriteLine("Reset");

            form_cmdlist_p.Invoke((EventHandler)(delegate
            {
                button_run_p.Text = "Run";
                RichTextBox_Command_p.Enabled = true;
                RichTextBox_Keyword_p.Enabled = true;

                RichTextBox_Command_p.Select(0, RichTextBox_Command_p.Text.Length);
                RichTextBox_Command_p.SelectionColor = Color.Black;
                RichTextBox_Command_p.SelectionFont = new Font(RichTextBox_Command_p.Font, FontStyle.Regular);

                RichTextBox_Keyword_p.Select(0, RichTextBox_Keyword_p.Text.Length);
                RichTextBox_Keyword_p.SelectionColor = Color.Black;
                RichTextBox_Keyword_p.SelectionFont = new Font(RichTextBox_Keyword_p.Font, FontStyle.Regular);
            }));
            
            command_highlight_offset = 0;
            cmdlist_command_cnt = 0;
            cmdlist_command_total = 0;
            cycle_cnt = 0;
            timer_execute.Enabled = false;
        }

        void Execute_Start()
        {
            Dbg.WriteLine("Execute start");

            form_cmdlist_p.Invoke((EventHandler)(delegate
            {
                button_run_p.Text = "Stop";
                RichTextBox_Command_p.Enabled = false;
                RichTextBox_Keyword_p.Enabled = false;
            }));

            command_highlight_offset = 0;
            cmdlist_command_cnt = 0;

            timer_execute.Interval = (int)Param.dict["CMDLIST_INTERVAL"] * 100;//单位是100ms
            timer_execute.Enabled = true;   //在execute定时器中，逐条执行命令
        }

        public void Run(Button button_run_s, RichTextBox RichTextBox_Command_s, RichTextBox RichTextBox_Keyword_s)
        {
            if(button_run_s.Text == "Run")
            {
                button_run_p = button_run_s;
                RichTextBox_Command_p = RichTextBox_Command_s;
                RichTextBox_Keyword_p = RichTextBox_Keyword_s;

                button_run_p.Text = "Cnt:" + count_down_count.ToString() + "...";
                timer_count_down.Enabled = true;    //发起count down定时，定时到了才开启execute定时器
            }
            else
            {
                timer_count_down.Enabled = false;
                count_down_count = INIT_COUNT_DOWN_VALUE;

                Reset();
            }
        }
    }
}
